package main

import (

  "fmt"
  "io/ioutil"
  "net/http"
   "crypto/tls"
    "crypto/x509"


  "net/url"
)


type Jar struct {

    cookies map[string][]*http.Cookie
}

func NewJar() *Jar {
    jar := new(Jar)
    jar.cookies = make(map[string][]*http.Cookie)
    return jar
}

// SetCookies handles the receipt of the cookies in a reply for the
// given URL.  It may or may not choose to save the cookies, depending
// on the jar's policy and implementation.
func (jar *Jar) SetCookies(u *url.URL, cookies []*http.Cookie) {

    jar.cookies[u.Host] = cookies

}

// Cookies returns the cookies to send in a request for the given URL.
// It is up to the implementation to honor the standard cookie use
// restrictions such as in RFC 6265.
func (jar *Jar) Cookies(u *url.URL) []*http.Cookie {
    return jar.cookies[u.Host]
}


func main() {
  

  jar := NewJar()

  a ,_:=url.Parse("https://git.uyunsoft.cn/show/show/graphs/master")

  pool :=x509.NewCertPool()
  caCertPath :="git.uyunsoft.cn.crt"
  caCrt, err := ioutil.ReadFile(caCertPath)
    if err != nil {
        fmt.Println("ReadFile err:", err)
        return
    }
   pool.AppendCertsFromPEM(caCrt)
    tr := &http.Transport{
        TLSClientConfig: &tls.Config{RootCAs: pool},
    }
  
    if err != nil {
        fmt.Println("Loadx509keypair err:", err)
        return
    }


        cookie1 :=http.Cookie{Name:"collapsed_nav",Value:"false"}
    cookie2 :=http.Cookie{Name:"_gitlab_session",Value:"b2a6a7ab78254a3262a16e9a407d8a19"}

  cookies := make([]*http.Cookie, 2)

   cookies = append(cookies,&cookie1)
     cookies = append(cookies,&cookie2)

  

    jar.SetCookies(a, cookies)
   client := &http.Client{Transport: tr,Jar:jar}

   fmt.Println(jar.Cookies(a));
   
   // 这里面在拿cookies时总是空指针异常不知道为什么
   // 但是顶上的实现 Client.Jar 是一个很好的例子
   res, err := client.Get("https://git.uyunsoft.cn/show/show/graphs/master") 

    if err != nil {
        fmt.Println("Get error:", err)
        return
    }
    defer res.Body.Close()
    body, err := ioutil.ReadAll(res.Body)
    if err != nil {
      fmt.Println("%s", err)
    }
      fmt.Println(body)
    }
